//DevNETCoder
//CopyRight By DevNET Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using STT.Entities;
using STT.DataAccess;
 
namespace STT.Business
{
    public class BTbTinh_TP
    {
        //-----------------------------------------------------------//
        public static bool KiemTraDuLieu(ETbTinh_TP Tinh_TP)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenTinh_TP", Tinh_TP.TenTinh_TP);
         
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbTinh_TP_KiemTraDuLieu", pr);
            if (all.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        //-----------------------------------------------------------//
        public static bool KiemTraDuLieu_Sua(ETbTinh_TP Tinh_TP)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"TenTinh_TP", Tinh_TP.TenTinh_TP);
            pr[1] = new SqlParameter(@"MaTinh_TP", Tinh_TP.MaTinh_TP);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbTinh_TP_KiemTraDuLieu_Sua", pr);
            if (all.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        //-----------------------------------------------------------//
        public static DataTable SearchTinh_TP(ETbTinh_TP Tinh_TP)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenTinh_TP", Tinh_TP.TenTinh_TP);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbTinh_TP_Search", pr);
            return all;
        }
        //-------------------------------------------------------------------
        public static DataTable SelectAll1()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbTinh_TP_SelectAll1", null);
            return all;
        }
//-----------------------------------------------------------//
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbTinh_TP_SelectAll", null);
            return all;
        }
 
//-----------------------------------------------------------//
        public static void Insert(ETbTinh_TP OTbTinh_TP)
        {
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter(@"MaTinh_TP", OTbTinh_TP.MaTinh_TP);
            pr[1] = new SqlParameter(@"TenTinh_TP", OTbTinh_TP.TenTinh_TP);
            pr[2] = new SqlParameter(@"TrangThai", OTbTinh_TP.TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbTinh_TP_Insert", pr);
        }
//-----------------------------------------------------------//
        public static void Update(ETbTinh_TP OTbTinh_TP)
        {
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter(@"MaTinh_TP", OTbTinh_TP.MaTinh_TP);
            pr[1] = new SqlParameter(@"TenTinh_TP", OTbTinh_TP.TenTinh_TP);
            pr[2] = new SqlParameter(@"TrangThai", OTbTinh_TP.TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbTinh_TP_Update", pr);
        }
//-----------------------------------------------------------//
        public static void Delete(int MaTinh_TP)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaTinh_TP",MaTinh_TP);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbTinh_TP_Delete", pr);
        }
//-----------------------------------------------------------//
        public static ETbTinh_TP SelectByID(int MaTinh_TP)
        {
            ETbTinh_TP OTbTinh_TP = new ETbTinh_TP();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaTinh_TP",MaTinh_TP);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure,"TbTinh_TP_SelectByID", pr);
            if (idr.Read())
              OTbTinh_TP = GetOneTbTinh_TP(idr);
            idr.Close();
            idr.Dispose();
            return OTbTinh_TP;
        }
//-----------------------------------------------------------//
        private static ETbTinh_TP GetOneTbTinh_TP(IDataReader idr)
       {
            ETbTinh_TP OTbTinh_TP = new ETbTinh_TP();
                if (idr["MaTinh_TP"] != DBNull.Value)
                    OTbTinh_TP.MaTinh_TP = (int)idr["MaTinh_TP"];
                if (idr["TenTinh_TP"] != DBNull.Value)
                    OTbTinh_TP.TenTinh_TP = (string)idr["TenTinh_TP"];
                if (idr["TrangThai"] != DBNull.Value)
                    OTbTinh_TP.TrangThai = (int)idr["TrangThai"];
            return OTbTinh_TP;
       }
//-----------------------------------------------------------//
        public static bool TestByID(int MaTinh_TP)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaTinh_TP",MaTinh_TP);
            pr[1] = new SqlParameter(@"TestID",SqlDbType.Bit);
            pr[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbTinh_TP_TestByID", pr);
            return Convert.ToBoolean(pr[1].Value);
        }
//-----------------------------------------------------------//
        public static List<ETbTinh_TP> ListAll()
        {
            List<ETbTinh_TP> list = new List<ETbTinh_TP>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbTinh_TP_SelectAll", null);
            while (idr.Read())
              list.Add(GetOneTbTinh_TP(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }
    }
 
}
